package com.ibm.cloud.banking.sample.service.impl;

import com.ibm.cloud.banking.sample.convert.OePbsLogMapper;
import com.ibm.cloud.banking.sample.dto.OePbsLogDTO;
import com.ibm.cloud.banking.sample.entity.OePbsLogEntity;
import com.ibm.cloud.banking.sample.entity.OePbsLogEntityPK;
import com.ibm.cloud.banking.sample.repository.ICustomizedSave;
import com.ibm.cloud.banking.sample.repository.OePbsLogRepository;
import com.ibm.cloud.banking.sample.service.IPbsLogService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import javax.annotation.Resource;

/**
 * replace desc in here
 *
 * @author HanDong
 * @date 2022/7/28
 */
@Service
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = RuntimeException.class)
public class PbsLogService implements IPbsLogService {


    @Resource
    private OePbsLogRepository oePbsLogRepository;

    @Resource
    private OePbsLogMapper oePbsLogMapper;


    @Resource
    private ICustomizedSave<OePbsLogEntity> customizedSave;

    @Override
    public OePbsLogDTO saveLog(OePbsLogDTO oePbsLogDTO) {

        return oePbsLogMapper.entityToDto(customizedSave.insert(
                oePbsLogMapper.dtoToEntity(oePbsLogDTO)
        ));

    }

    @Override
    public void updateLog(OePbsLogEntityPK id, String response) {
        Optional<OePbsLogEntity> optionalOePbsLogEntity = oePbsLogRepository.findById(id);
        if (optionalOePbsLogEntity.isPresent()) {
            OePbsLogEntity oePbsLogEntity = optionalOePbsLogEntity.get();
            oePbsLogEntity.setPbsResult(response);
            oePbsLogRepository.save(oePbsLogEntity);
        }
    }
}
